using Digitalmes.Application.Mes.Productions.WorkOrders;
using Digitalmes.Application.Mes.Productions.WorkOrders.Commands;
using Digitalmes.Application.Mes.Productions.WorkOrders.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Productions;

/// <summary>
/// 工单终结点。
/// </summary>
public sealed class WorkOrderEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/production", "workorder")
            .RequireAuthorization()
            .MapGet(GetWorkOrderListAsync, "list")
            .MapGet(GetWorkOrderPagedListAsync, "list-page")
            .MapGet(GetScheduleWorkOrderListQuery, "schedule/list")
            .MapGet(GetSequenceWorkOrderPagedListQuery, "sequence/list-page")
            .MapPost(CreateWorkOrderAsync)
            .MapPut(UpdateWorkOrderAsync, "{id}")
            .MapPut(IssueWorkOrderAsync, "issue/{id}")
            .MapPut(ScheduleWorkOrderAsync, "schedule/{id}")
            .MapPut(SortWorkOrderAsync, "sort")
            .MapPut(HoldOnWorkOrderAsync, "holdon/{id}")
            .MapPut(CancelWorkOrderAsync, "cancel/{id}")
            .MapPut(CloseWorkOrderAsync, "close/{id}");
    }

    [EndpointDescription("查询工单集合")]
    public async Task<IApiResult> GetWorkOrderListAsync(ISender sender, [AsParameters] GetWorkOrderListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询工单")]
    public async Task<IApiResult> GetWorkOrderPagedListAsync(ISender sender, [AsParameters] GetWorkOrderListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("查询工单排程集合")]
    public async Task<IApiResult> GetScheduleWorkOrderListQuery(ISender sender, [AsParameters] GetScheduleWorkOrderListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询生产序列")]
    public async Task<IApiResult> GetSequenceWorkOrderPagedListQuery(ISender sender, [AsParameters] GetSequenceWorkOrderListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建工单")]
    public async Task<IApiResult> CreateWorkOrderAsync(ISender sender, ProdWorkOrderDto dto)
    {
        var result = await sender.Send(new CreateWorkOrderCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新工单")]
    public async Task<IApiResult> UpdateWorkOrderAsync(ISender sender, long id, ProdWorkOrderDto dto)
    {
        var result = await sender.Send(new UpdateWorkOrderCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("下发工单")]
    public async Task<IApiResult> IssueWorkOrderAsync(ISender sender, long id)
    {
        var result = await sender.Send(new IssueWorkOrderCommand(id));
        return result.ToApiResult();
    }

    [EndpointDescription("工单排产")]
    public async Task<IApiResult> ScheduleWorkOrderAsync(ISender sender, long id)
    {
        var result = await sender.Send(new ScheduleWorkOrderCommand(id));
        return result.ToApiResult();
    }

    [EndpointDescription("工单生产排序")]
    public async Task<IApiResult> SortWorkOrderAsync(ISender sender, SortWorkOrderCommand command)
    {
        var result = await sender.Send(command);
        return result.ToApiResult();
    }

    [EndpointDescription("暂停/恢复工单")]
    public async Task<IApiResult> HoldOnWorkOrderAsync(ISender sender, long id)
    {
        var result = await sender.Send(new HoldOnWorkOrderCommand(id));
        return result.ToApiResult();
    }

    [EndpointDescription("取消工单")]
    public async Task<IApiResult> CancelWorkOrderAsync(ISender sender, long id)
    {
        var result = await sender.Send(new CancelWorkOrderCommand(id));
        return result.ToApiResult();
    }

    [EndpointDescription("关闭工单")]
    public async Task<IApiResult> CloseWorkOrderAsync(ISender sender, long id)
    {
        var result = await sender.Send(new CloseWorkOrderCommand(id));
        return result.ToApiResult();
    }
}
